package andriod.action.file;

import com.jcrj.action.BaseAction;
import com.jcrj.entity.OFile;
import com.jcrj.entity.OUser;
import com.jcrj.service.FileService;
import com.jcrj.util.FtpUploadUtil;
import com.jcrj.util.SysConfig;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

public class ArdFileAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private FileService fileService;
	private File file;
	private String fileFileName;
	private String fileId;
	private String fileIds;
	private String fileArray;

	public String getFileList() {
		try {
			List list = this.fileService.getFileList(this.fileArray);
			JSONObject json = new JSONObject();
			json.put("fileList", list);
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(json.toString());
			super.getResponse().getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String fileUpload() {
		PrintWriter writer = null;
		try {
			writer = getResponse().getWriter();
			OUser user = (OUser) super.getSession().get("userInfo");
			FtpUploadUtil load = new FtpUploadUtil();
			System.out.println(getRequest().getParameter("fileFileName"));
			String pathFile = load.uploadFile(this.file, this.fileFileName,
					user.getAccount());
			if ((pathFile != null) && (!"".equals(pathFile))) {
				OFile oFile = new OFile();
				oFile.setName(this.fileFileName);
				oFile.setUrl(pathFile);
				oFile.setFileSize(Long.valueOf(this.file.length()));
				oFile.setCreateTime(new Date());
				this.fileService.saveFile(oFile);
				writer.write(oFile.getId().toString());
				writer.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String fileUploadImg() {
		try {
			FtpUploadUtil load = new FtpUploadUtil();
			String path = SysConfig.getInstance().getProperty("image.upload");
			String down = SysConfig.getInstance().getProperty("image.load");
			String pathFile = load.uploadFile(this.file, this.fileFileName,
					path);
			if ((pathFile != null) && (!"".equals(pathFile))) {
				OFile oFile = new OFile();
				oFile.setName(this.fileFileName);
				oFile.setUrl(down + pathFile);
				oFile.setForeign(Integer.valueOf(4));
				oFile.setCreateTime(new Date());
				this.fileService.saveFile(oFile);
				super.getResponse().getWriter().write(oFile.getId().toString());
				super.getResponse().getWriter().flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String downLoadFile() {
		try {
			OFile ofile = this.fileService.getFileById(Integer
					.parseInt(this.fileId));
			if (ofile != null) {
				FtpUploadUtil load = new FtpUploadUtil();
				load.downLoadFile(super.getResponse(), ofile.getUrl(),
						ofile.getName());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public String deleteFile() {
		try {
			this.fileService.deleteFileById(this.fileIds);
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public FileService getFileService() {
		return this.fileService;
	}

	public void setFileService(FileService fileService) {
		this.fileService = fileService;
	}

	public File getFile() {
		return this.file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileId() {
		return this.fileId;
	}

	public void setFileId(String fileId) {
		this.fileId = fileId;
	}

	public String getFileIds() {
		return this.fileIds;
	}

	public void setFileIds(String fileIds) {
		this.fileIds = fileIds;
	}

	public String getFileArray() {
		return this.fileArray;
	}

	public void setFileArray(String fileArray) {
		this.fileArray = fileArray;
	}

	public String getFileFileName() {
		return this.fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
}